package pr.iceworld.fernando.oms.service;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import pr.iceworld.fernando.entity.User;
import pr.iceworld.fernando.oms.repository.UserRepository;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {

    @Resource
    UserRepository userRepository;
    @Resource
    PasswordHelper passwordHelper;

    public List<User> getAll() {
        return userRepository.selectAll();
    }


    public User getOne(User user) {
        return userRepository.selectOne(user);
    }


    public List<User> getUserRolePermission(User user) {
        return userRepository.selectUserRolePermission(user);
    }


    public User getUserByIdOrUsernameOrEmail(Long id, String username, String email) {
        return userRepository.selectUserByIdOrUsernameOrEmail(id, username, email);
    }


    public User createUser(User user) {
        passwordHelper.encryptPassword(user);
        return null;
    }


    public void update(User user) {
        userRepository.update(user);
    }

    /**
     * 修改密码
     * @param user
     */
    public void changePassword(User user) {
        User oldUser = userRepository.selectOne(user);
        oldUser.setPassword(user.getPassword());
        passwordHelper.encryptPassword(oldUser);
        update(oldUser);
    }
}
